package com.capgemini.cn.authority.rest.resource;

import com.capgemini.cn.authority.rest.exception.AuthRestBusinessException;
import com.capgemini.cn.authority.rest.service.AssetService;
import com.capgemini.cn.authority.rest.valueobject.DataResponse;
import com.capgemini.cn.authority.rest.valueobject.DataStatus;
import com.capgemini.cn.authority.rest.valueobject.data.AssetVO;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 * Created at 2017/11/1
 *
 * @author Andriy
 */
@RestController
@RequestMapping(path = BaseResource.RESOURCE_PREFIX + "asset")
public class AssetResource extends BaseResource<AssetVO> {

    @Autowired
    private AssetService assetService;

    @ApiIgnore(value = "该方法未实现故不在接口文档中出现")
    @Override
    public DataResponse<AssetVO> findByOne(long id) {
        return null;
    }

    @ApiOperation(value = "查询所有资源信息")
    @Override
    public DataResponse<List<AssetVO>> findAll(@ApiParam(value = PAGE_PARAM_COMMENT, name = PAGE_PARAM, required = true) @RequestParam(value = PAGE_PARAM) int page,
                                               @ApiParam(value = SIZE_PARAM_COMMENT, name = SIZE_PARAM, required = true) @RequestParam(value = SIZE_PARAM) int size,
                                               @ApiParam(value = ORDER_PARAM_COMMENT, name = ORDER_PARAM, required = true) @RequestParam(value = ORDER_PARAM) String order,
                                               @ApiParam(value = DIRECTION_PARAM_COMMENT, name = DIRECTION_PARAM, required = true) @RequestParam(value = DIRECTION_PARAM) Sort.Direction direction) {
        return this.assetService.findAll(page, size, order, direction).setDataStatus(DataStatus.SUCCESS);
    }

    @ApiIgnore(value = "该方法未实现故不在接口文档中出现")
    @Override
    public DataResponse remove(long id) throws AuthRestBusinessException {
        return null;
    }

    @ApiOperation(value = "查询指定权限下所有的资源数据")
    @GetMapping(path = "byAuth/{authId}")
    public DataResponse<List<AssetVO>> findAllByAuthority(@ApiParam(value = "权限主键ID值", name = "authId", required = true) @PathVariable(value = "authId") String authId) {
        return this.assetService.findAssetsByAuthorize(Long.valueOf(authId)).setDataStatus(DataStatus.SUCCESS);
    }
}
